package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "C端-购物车接口")
@RequestMapping("user/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @RequestMapping("add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        try {
            shoppingCartService.addShoppingCart(shoppingCartDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("添加购物车失败：", e);
            return Result.error("添加购物车失败: " + e.getMessage());
        }
}
    @GetMapping("list")
    @ApiOperation("查看购物车")
    public Result< List<ShoppingCart>> list() {
        log.info("查看购物车");
        List<ShoppingCart> list =shoppingCartService.showShoppingCart();
        return  Result.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @RequestMapping("clean")
    @ApiOperation("清空购物车")
    public Result cleanShoppingCart() {
        log.info("清空购物车");
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    /**
     * 删除购物车中的商品
     *
     * @param shoppingCartDTO
     * @return
     */
    @RequestMapping("sub")
    @ApiOperation("删除购物车中的商品")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中的商品：{}", shoppingCartDTO);
        try {
            shoppingCartService.subShoppingCart(shoppingCartDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("删除购物车中的商品失败：", e);
            return Result.error("删除购物车中的商品失败: " + e.getMessage());
        }
    }


}
